using FluentValidation;

using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;

using WL.Think.Abp.Validators;
using WL.Think.Sample.Books;

namespace WL.Think.Sample.Validators
{
    public class BookValidator : ThinkBaseValidator<CreateUpdateBookDto>
    {
        public BookValidator(IAbpLazyServiceProvider serviceProvider) : base(serviceProvider)
        {
        }

        protected override void CreateRule()
        {
            RuleFor(x => x.Name).Length(3, 6).WithMessage(L["Name Must Between 3 And 6"]);
            RuleFor(x => x.Name).CustomAsync(async (x, c, t) =>
            {
                var repo = GetRequiredService<IRepository<BookEntity, Guid>>();
                if (await repo.AnyAsync(s => s.Name == x))
                {
                    c.AddFailure(L["Name is Exists"]);
                }
            });
        }
    }
}
